package com.example.peibei.ui.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dingtao.common.bean.HomeBannerCate;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.CancellationPresenter;
import com.example.peibei.ui.weight.CenterDialog;

import butterknife.OnClick;

public class CancellationActivity extends WDActivity {

    private CancellationPresenter cancellationPresenter;
    private SPUtils spUtils;
    private String token;
    private int autoId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cancellation;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(CancellationActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        autoId = spUtils.getInt(SpConstant.AUTOID);
        cancellationPresenter = new CancellationPresenter(new LoginOutCall());
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.bt_submit)
    public void submit() {
        showLoginOutDialog();
    }

    class LoginOutCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            spUtils.clear();
            intent(LoginActivity.class);
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    private void showLoginOutDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_center_view, null);
        CenterDialog bottomDialog = new CenterDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText("确定销毁当前账号吗？");
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        tv_sure.setOnClickListener(v -> {
            cancellationPresenter.reqeust("Bearer " + token,autoId);
            bottomDialog.dismiss();
        });
    }

    @Override
    protected void destoryData() {

    }
}
